package com.ruoyi.lm.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lm.domain.*;
import com.ruoyi.lm.service.*;
import com.ruoyi.lm.service.impl.WxTemplateMsgService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务需求活动报名Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/phone/lm/usersign")
public class LmUsersignControllerPhone extends BaseController
{

    @Autowired
    private ILmUsersignService lmUsersignService;
    @Autowired
    private ILmUserService lmUserService;
    @Autowired
    private ILmTimesrecordService lmTimesrecordService;
    @Autowired
    private ILmRequireService lmRequireService;
    @Autowired
    private ILmSerinfoService lmSerinfoService;
    @Autowired
    private ILmActivityService lmActivityService;

    //微信支付商户开通后 微信会提供appid和appsecret和商户号partner
    @Value("${wxConfig.appId}")
    private String appId;

    @Value("${wxConfig.appSecret}")
    private String appSecret;

    /**
     * 查询服务需求活动报名列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody LmUsersign lmUsersign)
    {
        List<LmUsersign> list = lmUsersignService.selectLmUsersignList(lmUsersign);
        List<LmUsersign> resultList =new ArrayList<>();
        for(LmUsersign temp :list){
            //判断当前是需求还是服务
            String signType = temp.getSignType();
            if("0".equals(signType)){
                //判断当前需求或者服务是否已经查看过用户信息
                LmTimesrecord lmTimesrecord = new LmTimesrecord();
                lmTimesrecord.setRequireId(temp.getSignId());
                lmTimesrecord.setUserId(temp.getUserId());
                if(lmTimesrecordService.selectLmTimesrecordList(lmTimesrecord).size()>0){
                    temp.setIsView("0");//0查看过，1没有查看过
                }else{
                    temp.setIsView("1");
                }
            }else if("1".equals(signType)){
                //判断当前需求或者服务是否已经查看过用户信息
                LmTimesrecord lmTimesrecord = new LmTimesrecord();
                lmTimesrecord.setServiceId(temp.getSignId());
                lmTimesrecord.setUserId(temp.getUserId());
                if(lmTimesrecordService.selectLmTimesrecordList(lmTimesrecord).size()>0){
                    temp.setIsView("0");//0查看过，1没有查看过
                }else{
                    temp.setIsView("1");
                }
            }
            resultList.add(temp);
        }
        return getDataTable(resultList);
    }

    /**
     * 查询我报名的服务需求活动列表
     */
    @PostMapping("/listInfo")
    @ResponseBody
    public TableDataInfo listInfo(String userId,String signType)
    {
       LmUsersign lmUsersign = new LmUsersign();
       lmUsersign.setUserId(userId);
       List<LmUsersign> lmUsersignList = lmUsersignService.selectLmUsersignList(lmUsersign);
       List<LmRequire> resultList = new ArrayList<>();
       List<LmSerinfo> resultList2 = new ArrayList<>();
       List<LmActivity> resultList3 = new ArrayList<>();
       for(LmUsersign temp :lmUsersignList){
           if("0".equals(temp.getSignType())){
               //查询报名的需求
               resultList.add(lmRequireService.selectLmRequireById(temp.getSignId()));
           }
           if("1".equals(temp.getSignType())){
               //查询报名的服务
               resultList2.add(lmSerinfoService.selectLmSerinfoById(temp.getSignId()));
           }
           if("2".equals(temp.getSignType())){
               //查询报名的活动
               resultList3.add(lmActivityService.selectLmActivityById(temp.getSignId()));
           }
       }
       if("0".equals(signType)){
           return getDataTable(resultList);
       }else if("1".equals(signType)){
           return getDataTable(resultList2);
       }else{
           return getDataTable(resultList3);
       }

    }

    /**
     * 新增保存服务需求活动报名
     */
    @Log(title = "服务需求活动报名", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody LmUsersign lmUsersign) throws Exception
    {
        //判断是否重复报名
        LmUsersign existUser = new LmUsersign();
        existUser.setUserId(lmUsersign.getUserId());
        existUser.setSignId(lmUsersign.getSignId());
        if(lmUsersignService.selectLmUsersignList(existUser).size()>0){
            return error("您已成功报名，请勿重复报名");
        }
        lmUsersign.setId(IdWorker.getId());
        if("2".equals(lmUsersign.getSignType())){
            //判断报名人员是否是活动发布人员
            LmActivity lmActivity = lmActivityService.selectLmActivityById(lmUsersign.getSignId());
            if(lmActivity.getUserId().equals(lmUsersign.getUserId())){
                return error("自己发布的活动不能报名！");
            }

            lmUsersign.setSignStatus("2");
            //判断活动人数是否已满
            LmUsersign temp = new LmUsersign();
            temp.setSignId(lmUsersign.getSignId());
            if(lmUsersignService.selectLmUsersignList(temp).size()+1 == lmActivity.getActivityMember()){
                //活动已满
                LmActivity newActi = new LmActivity();
                newActi.setId(lmActivity.getId());
                newActi.setActivityStatus("3");
                lmActivityService.updateLmActivity(newActi);
            }
        }else if("0".equals(lmUsersign.getSignType())){
           //自己发布的需求不能自己报名
            LmRequire lmRequire = lmRequireService.selectLmRequireById(lmUsersign.getSignId());
            if(lmRequire.getUserId().equals(lmUsersign.getUserId())){
                return error("自己发布的需求不能自己报名！");
            }
            lmUsersign.setSignStatus("0");
        }else{
            LmSerinfo lmSerinfo = lmSerinfoService.selectLmSerinfoById(lmUsersign.getSignId());
            if(lmSerinfo.getUserId().equals(lmUsersign.getUserId())){
                return error("自己发布的服务不能自己报名！");
            }
        }
        String text = "";
        String userId = "";
        switch (lmUsersign.getSignType()){
            case "0":text = "需求";
                LmRequire require = lmRequireService.selectLmRequireById(lmUsersign.getSignId());
                userId = require.getUserId();
                break;
            case "1":text = "服务";
                LmSerinfo serinfo = lmSerinfoService.selectLmSerinfoById(lmUsersign.getSignId());
                userId = serinfo.getUserId();
                break;

            case "2":text = "活动";
                LmActivity activity = lmActivityService.selectLmActivityById(lmUsersign.getSignId());
                userId = activity.getUserId();
                break;
        }
//        LmUser user = lmUserService.selectLmUserById(lmUsersign.getUserId());
        LmUser user = lmUserService.selectLmUserById(userId);
        if(user != null){
            WxTemplateMsgService wxTemplateMsgService = new WxTemplateMsgService();
            wxTemplateMsgService.send_template_message(user.getOpenId(), appId, appSecret, text);
        }
        return toAjax(lmUsersignService.insertLmUsersign(lmUsersign));
    }
    /**
     * 修改保存服务需求活动报名
     */
    @Log(title = "服务需求活动报名", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LmUsersign lmUsersign)
    {
        return toAjax(lmUsersignService.updateLmUsersign(lmUsersign));
    }

    /**
     * 删除服务需求活动报名
     */
    @Log(title = "服务需求活动报名", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lmUsersignService.deleteLmUsersignByIds(ids));
    }
}
